package com.lekang.service.impl;

import java.util.List;
import com.lekang.common.utils.DateUtils;
import com.lekang.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.mapper.NursingItemMapper;
import com.lekang.domain.NursingItem;
import com.lekang.service.INursingItemService;

/**
 * 护理项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-14
 */
@Service
public class NursingItemServiceImpl implements INursingItemService 
{
    @Autowired
    private NursingItemMapper nursingItemMapper;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingItem selectNursingItemById(Long id)
    {
        return nursingItemMapper.selectNursingItemById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingItem 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingItem> selectNursingItemList(NursingItem nursingItem)
    {
        return nursingItemMapper.selectNursingItemList(nursingItem);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingItem 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingItem(NursingItem nursingItem)
    {
        // 校验护理项目名称是否重复
        if (StringUtils.isNotEmpty(nursingItem.getName())) {
            NursingItem existItem = nursingItemMapper.selectNursingItemByName(nursingItem.getName());
            if (existItem != null) {
                throw new RuntimeException("护理项目名称已存在");
            }
        }
        nursingItem.setCreateTime(DateUtils.getNowDate());
        return nursingItemMapper.insertNursingItem(nursingItem);
    }

    /**
     * 修改护理项目
     * 
     * @param nursingItem 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingItem(NursingItem nursingItem)
    {
        // 校验护理项目名称是否重复（排除自身）
        if (StringUtils.isNotEmpty(nursingItem.getName())) {
            NursingItem existItem = nursingItemMapper.selectNursingItemByName(nursingItem.getName());
            if (existItem != null && !existItem.getId().equals(nursingItem.getId())) {
                throw new RuntimeException("护理项目名称已存在");
            }
        }
        return nursingItemMapper.updateNursingItem(nursingItem);
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingItemByIds(Long[] ids)
    {
        return nursingItemMapper.deleteNursingItemByIds(ids);
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingItemById(Long id)
    {
        return nursingItemMapper.deleteNursingItemById(id);
    }

    @Override
    public List<NursingItem> selectEnableNursingItems() {
        NursingItem query = new NursingItem();
        query.setStatus(1L);
        return nursingItemMapper.selectNursingItemList(query);
    }

    @Override
    public List<NursingItem> selectNursingItemByLikeName(String name) {
        return nursingItemMapper.selectNursingItemByLikeName(name);
    }
}
